package com.expense.app.admin.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Date;

@Configuration
public class DataSourceConfig {

	private static final Log log = LogFactory.get();

	@Value("${spring.datasource.url}")
	private String url;
//	@Value("${spring.datasource.driver-class-name}")
//	private String driverClassName;
	@Value("${spring.datasource.username}")
	private String userName;
	@Value("${spring.datasource.password}")
	private String password;
	@Value("${spring.datasource.initialSize}")
	private Integer initialSize;
	@Value("${spring.datasource.minIdle}")
	private Integer minIdle;
	@Value("${spring.datasource.maxActive}")
	private Integer macActive;
	@Value("${spring.datasource.maxWait}")
	private Integer maxWait;
	@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
	private Integer timeBetweenEvictionRunsMillis;
	@Value("${spring.datasource.minEvictableIdleTimeMillis}")
	private Integer minEvictableIdleTimeMillis;
	@Value("${spring.datasource.validationQuery}")
	private String validationQuery;
	@Value("${spring.datasource.testWhileIdle}")
	private Boolean testWhileIdle;
	@Value("${spring.datasource.testOnBorrow}")
	private Boolean testOnBorrow;
	@Value("${spring.datasource.testOnReturn}")
	private Boolean testOnReturn;

	@Value("${spring.datasource.poolPreparedStatements}")
	private Boolean poolPreparedStatements;
	@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
	private Integer maxPoolPreparedStatementPerConnectionSize;
	@Value("${spring.datasource.filters}")
	private String filters;

	@Bean(name = "dataSource")
	public DataSource datasource() {
		HikariDataSource ds = new HikariDataSource();
//		HikariDataSource ds = new HikariDataSource();
		try {
			ds.setJdbcUrl(url);
			ds.setUsername(userName);
			ds.setPassword(password);
//			ds.setDriverClassName(driverClassName);
//			ds.setInitializationFailTimeout(initialSize);
//			ds.setMinimumIdle(minIdle);
//			ds.setMaximumPoolSize(macActive);
//			ds.setMaxLifetime(maxWait);
//			ds.setTime(timeBetweenEvictionRunsMillis);
//			ds.setId(minEvictableIdleTimeMillis);
//			ds.setValidationQuery(validationQuery);
//			ds.setTestWhileIdle(testWhileIdle);
//			ds.setTestOnBorrow(testOnBorrow);
//			ds.setTestOnReturn(testOnReturn);
//			ds.setPoolPreparedStatements(poolPreparedStatements);
//			ds.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
//			ds.setFilters(filters);
		}catch (Exception e) {
			log.error("初始化数据库异常{}，时间为{}",e.getMessage(), DateUtil.format(new Date(),"yyy-MM-dd hh:mm:ss"));
		}
		return ds;
	}

//	@Bean
//	public FilterRegistrationBean filterRegistrationBean() {
//		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//		filterRegistrationBean.setFilter(new WebStatFilter());
//		filterRegistrationBean.addUrlPatterns("/*");
//		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//		return filterRegistrationBean;
//	}
}



